package com.fh.common.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component // 将当前类交给ioc管理
public class KuayuInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        /* 解决跨域     response.setHeader   */

        //获取请求域
        String origin = request.getHeader("Origin");
        //设置允许跨域
        response.setHeader("Access-Control-Allow-Origin",origin);

        System.out.println(request.getMethod());

        if(request.getMethod().equals("OPTIONS")){ //是否允许修改header信息
            response.setHeader("Access-Control-Allow-Headers","token");
            response.setStatus(HttpServletResponse.SC_OK);
            return false;
        }

        return true;
    }
}
